
bt.module({builder: function(bt) {
    var TOTAL = 4;
    var current = TOTAL - 1;
    var interval = 5000; //ms
    var timer;
    var _changePosterBgToCurrent = function() {
        j$(".navi .item").removeClass("current");
        j$(".scrollable .items .current").stop(true, true)
                .removeClass("current").fadeOut(
            function() {
                current = (current + 1) % TOTAL;
                j$(".scrollable .items .bg" + current).stop(true, true).addClass("current").fadeIn();
                j$(".navi .n" + current).addClass("current");
            });
    };
    var _changePosterBg = function() {
        _changePosterBgToCurrent();
        clearTimeout(timer);
        timer = null;
        timer = setTimeout(function() {
            _changePosterBg();
        }, interval);
    };

    var _ellipsisHotDestnations = function() {
        var ellipsis_opts = {max_rows: 1};
        // hot_destinations name
        j$(".hot-destinations .photos .photo .mask .name").each(function() {
            if(!j$(this).attr("threedots")) {
                j$(this).ThreeDots(ellipsis_opts);
            }
        });
    }

    var _initLoadMoreEvent = function() {
        j$("#load-more").click(function() {
            var _this = this;
            j$('#ajax-more').css("visibility", "visible");

            j$.ajax({
                url: "/hot_destinations/",
                data: "start=" + j$(_this).data("start") + "&count=" + j$(_this).data("count"),
                success: function(resp) {
                    if(resp) {
                        j$(".hot-destinations .photos").append(resp);
                    } else {
                        j$("#load-more").slideUp();
                    }
                }
            }).done(function() {
                _ellipsisHotDestnations();
                j$(_this).remove();
                _initLoadMoreEvent();
            });
        });
    };

    j$(document).ready(function() {
        _changePosterBg();
        _ellipsisHotDestnations();
        _initLoadMoreEvent();
        j$(".navi .item").hover(function() {
            clearTimeout(timer);
            timer = null;
            var _current = parseInt(j$(this).data("num"));
            if(current != _current) {
                current = (_current + TOTAL - 1) % TOTAL;
                _changePosterBgToCurrent(); 
            }
        }, function() {
            clearTimeout(timer);
            timer = null;
            timer = setTimeout(function() {
                _changePosterBg();
            }, interval);
        });
    });
}});
